<?php

/**
 * Created on 7 mai 2011
 *  * @author Samy Deghou(deghou@polytech.unice.fr ; deghou@embl.de)
 * 		   Stephan Beisken (beisken@ebi.ac.uk)
 * 		   Luis Felipe De Figueiredo (ldpf@ebi.ac.uk)
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

require_once("/var/www/chemistry-repository/chemistry-repository/trunk/main/webapp/Model/PajaxRemote.class.php");

class Molecule  extends PajaxRemote{
    /*
     * Attributes
     */

    private $_moleculeID;
    private $_date;
    private $_name;
    private $_inchiKey;
    private $_inchi;
    private $_fingerprint;
    private $_chebiID;
    private $_exactMass;

    /*
     * Constructor
     */



	public function Molecule(){
		$this->_date = null;
        $this->_name = null;
        $this->_inchiKey = null;
        $this->_inchi = null;
        $this->_fingerprint = null;
        $this->_chebiID = null;
        $this->_exactMass = null;
        $this->_moleculeID = -1;
		
	}
	
	
    public function __Molecule($date, $name, $inchiKey, $inChi, $fingerprint, $chebiID, $exactMass, $moleculeID=-1) {
        $this->_date = $date;
        $this->_name = $name;
        $this->_inchiKey = $inchiKey;
        $this->_inchi = $inChi;
        $this->_fingerprint = $fingerprint;
        $this->_chebiID = $chebiID;
        $this->_exactMass = $exactMass;
        $this->_moleculeID = $moleculeID;
    }

    /*
     * Accessors
     */

    public function getmoleculeID() {
        return $this->_moleculeID;
    }

    public function getTheDate() {
        return $this->_date;
    }

    public function getName() {
        return $this->_name;
    }

    public function getInChiKey() {
        return $this->_inchiKey;
    }

    public function getInChi() {
        return $this->_inchi;
    }

    public function getFingerprint() {
        return $this->_fingerprint;
    }

    public function getChebiId() {
        return $this->_chebiID;
    }

    public function getExactMass() {
        return $this->_exactMass;
    }

    /*
     * Modifiers
     */

    public function setmoleculeID() {
        return $this->_moleculeID;
    }

    public function setTheDate($param) {
        $this->_date = $param;
    }

    public function setName($param) {
        $this->_name = $param;
    }

    public function setInChiKey($param) {
        $this->_inchiKey = $param;
    }

    public function setInChi($param) {
        $this->_inChi = $param;
    }

    public function setFingerprint($param) {
        $this->_fingerprint = $param;
    }

    public function setChebiId($param) {
        $this->_chebiID = $param;
    }

    public function setExactMass($param) {
        $this->_exactMass = $param;
    }

    /*
     * CRUD
     */

    /**
     * Put an object in the database
     */
    public function create() {
        $connection=Database::connect();
        if ($this->_moleculeID != -1)
            die("Unable to create, still existing !");
        else {
   //     	echo($this->_date." ; ".$this->_name." ; ".$this->_inchiKey." ; ".$this->_inchi." ; ".$this->_fingerprint." ; ".$this->_chebiID. " ; ".$this->_exactMass);
            $query = "INSERT INTO `MOLECULE` values (NULL,'" .
                    $this->_date . "','" .
                    $this->_name . "','" .
                    $this->_inchiKey . "','" .
                    $this->_inchi . "','" .
                    $this->_fingerprint . "','" .
                    $this->_chebiID . "','" .
                    $this->_exactMass . "')";
            mysql_query($query) or die(mysql_error());
            $this->_moleculeID = mysql_insert_id();
//                    	echo("The molecule: ".$this->_name." has been created with the id--> ".$this->_moleculeID);
        }
        Database::disconnect($connection);
    }

    /**
     * Read an object from the database
     */
    public static function read($key) {
        $connection=Database::connect();
        $query = "SELECT * FROM MOLECULE WHERE molecule_id = '" . $key . "'";
        $ans = mysql_fetch_assoc(mysql_query($query));
        extract($ans);
        $molecule = new Molecule();
        $molecule->__Molecule($date, $name, $inchi_key, $inchi, $fingerprint, $chebi_id, $exact_mass, $molecule_id);
        Database::disconnect($connection);
        return $molecule;
    }
    
    
    public static function readFromChebiId($chebiId){
    	$connection=Database::connect();
        $query = "SELECT * FROM MOLECULE WHERE chebi_id = '" . $chebiId . "'";
        $ans = mysql_fetch_assoc(mysql_query($query));
        extract($ans);
        $molecule = new Molecule();
        $molecule->__Molecule($date, $name, $inchi_key, $inchi, $fingerprint, $chebi_id, $exact_mass, $molecule_id);
        Database::disconnect($connection);
        return $molecule;
    }
    
    

    /**
     * Update an object from the database
     */
    public function update() {
        $connection=Database::connect();
        $connection=Database::connect();
        $query = "UPDATE `MOLECULE` SET date='" .
                $this->_date . "', name='" .
                $this->_name . "', inchi_key='" .
                $this->_inchiKey . "', inchi='" .
                $this->_inchi . "', fingerprint='" .
                $this->_fingerprint . "', chebi_id='" .
                $this->_chebiID . "', exact_mass='" .
                $this->_exactMass . "' WHERE molecule_id='" .
                $this->_moleculeID . "'";
        mysql_query($query) or die(mysql_error());
        Database::disconnect($connection);
    }

    /**
     * Delete an object from the database
     */
    public function delete() {
        $connection=Database::connect();
        $query = "DELETE FROM `MOLECULE` WHERE molecule_id='" .
                $this->_moleculeID . "'";
        mysql_query($query) or die(mysql_error());
        Database::disconnect($connection);
    }

}
?>
